$(function() {
	var answerColumn = $(".answer");
	$.scrollUp({
		scrollName: "scrollUp",
		topDistance: "300",
		topSpeed: 300,
		animation: "fade",
		animationInSpeed: 200,
		animationOutSpeed: 200,
		scrollText: '<i class="fa fa-angle-up"></i>',
		activeOverlay: !1
	})
	$("#submit").click(function(e) {
		var values = [];
		/* -- 获取所有问题 -- */
		$("#questions tbody tr").each(function(index, element) {
			var val = $(element).find("input").val();
			var idWithType = $(element).attr("attr");
			var idElems = idWithType.split('-', 2);
			if ($.trim(val)) {
				var value = {
					"id": parseInt(idElems[1]),
					"val": val,
					"elemType": idElems[0]
				};
				values.push(value);
			} else {
				$(element).addClass("warning");
				var qResultWarningBtn = $(element).find(".answer button");
				clearBtnStatus(qResultWarningBtn);
				qResultWarningBtn.addClass("btn-warning");
				qResultWarningBtn.hide();
			}
		});
		if (values.length > 0) {
			$.ajax({
				url: 'checkAnswer',
				type: 'POST',
				dataType: "text json",
				data: {
					"answers": JSON.stringify(values)
				},
				success: function(data) {
					if (data.state == "success") {
						$.each(data.errors, function(index, element) {
							var idWithType = element.elemType + '-' + element.id;
							$("#questions tbody [attr='" + idWithType + "']").addClass('danger');
						});
					} else if (data.state == "error") {
						$.each(data.errors, function(index, element) {
							var idWithType = element.elemType + '-' + element.id;
							var questionRow = $("#questions tbody [attr='" + idWithType + "']");
							questionRow.addClass('danger');
							var qResultBtn = questionRow.find(".answer button");
							clearBtnStatus(qResultBtn);
							qResultBtn.addClass("btn-danger");
							qResultBtn.show();
							questionRow.find(".answer button").attr("data-content", element.val);
						});
						/* -- 为所有非Warning和Danger的行,添加Success,并隐藏 -- */
						var qSuccessRows = $("#questions tbody tr:not(.danger, .warning)");
						qSuccessRows.addClass("success").hide();
						var qSuccessResultBtn = qSuccessRows.find(".answer button");
						clearBtnStatus(qSuccessResultBtn);
						qSuccessResultBtn.addClass("btn-success");
						qSuccessResultBtn.hide();
						/* -- 显示答案列 -- */
						answerColumn.show();
						/* -- 提示弹出框初始化, 注意!影响性能 --  */
						$('[data-toggle="popover"]').popover();
						showResultPanel();
					}
				},
				error: function(XMLHttpRequest, textStatus, errorThrown) {
					alert("XMLHttpRequest.status : " + XMLHttpRequest.status +
						"\nXMLHttpRequest.readyState : " + XMLHttpRequest.readyState +
						"\ntextStatus : " + textStatus
					);
				}
			});
		}
	});
	$("#show-all").click(function(){
		if ($(this).text() == "Show All Result")
			$(this).text("Hide Success");
		else
			$(this).text("Show All Result");
		$("#questions tbody tr.success").toggle();
	});
	function showResultPanel() {
		/* -- 问题数量 -- */
		var questionCount = $("#questions tbody tr").size();
		var errorCount = $("#questions tbody tr.danger").size();
		var warningCount = $("#questions tbody tr.warning").size();
		var errorPercent = "" + errorCount * 100 / questionCount + "%";
		var warningPercent = "" + warningCount * 100 / questionCount + "%";
		var successPercent = "" + (questionCount - errorCount - warningCount) * 100 / questionCount + "%";
		$("#success-count").css("width", successPercent);
		$("#success-count").text(successPercent);
		$("#error-count").css("width", errorPercent);
		$("#error-count").text(errorPercent);
		$("#warning-count").css("width", warningPercent);
		$("#warning-count").text(warningPercent);
		var resultPane = $("#result");
		resultPane.show();
		$("html,body").animate({
			scrollTop: resultPane.offset().top - 100
		}, 200);
	}
	function clearBtnStatus(elem)
	{
		elem.removeClass("btn-success");
		elem.removeClass("btn-warning");
		elem.removeClass("btn-danger");
	}
	function clearStatus(elem)
	{
		elem.removeClass("success");
		elem.removeClass("warning");
		elem.removeClass("danger");
	}
	/* -- Function End -- */
});
